package com.example.hotel.ease.controller;

import com.example.hotel.ease.pojo.entity.User;
import com.example.hotel.ease.pojo.param.LoginParam;
import com.example.hotel.ease.pojo.vo.LoginResult;
import com.example.hotel.ease.service.IUserService;
import com.example.hotel.ease.web.JsonResult;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/member")
public class MemberController {

    @Autowired
    private IUserService userService;

    @PostMapping("/login")
    public JsonResult login(LoginParam param, HttpSession session) {
        LoginResult loginResult = userService.login(param,  User.UserType.MEMBER);
        loginResult.setType(null);

        session.setAttribute("loginMember", loginResult);

        return JsonResult.ok(loginResult);
    }

    @PostMapping("/logout")
    public JsonResult logout(HttpSession session) {
        session.invalidate();
        return JsonResult.ok();
    }

}
